package com.txl.web.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.txl.domain.UserBean;
import com.txl.service.UserService;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		UserService service = new UserService();
		UserBean user = service.login(username, password);
		if(user!=null){
			request.getSession().setAttribute("user", user);
			//让用户登陆成功后跳转到首页
			response.sendRedirect(request.getContextPath()+"/index.jsp");
			return;
		}
		
		request.setAttribute("message", "用户名或密码错误!");
		request.getRequestDispatcher("/jsps/login.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
